import React, {Component, Fragment} from 'react';
import {LocalStorage} from '@/utils/storage';
import {BTN_PERMISSIONS} from '@/common/Constants';
import {Button, Menu} from 'antd';
import TOButton from "@/components/TableList/TOButton";

export let wrapAuth = ComposedComponent => class WrapComponent extends Component {

  render() {
    if (this.props.isHide) {
      return null;
    }
    if (getAuth(this.props.auth) || !this.props.auth) {
      return <ComposedComponent {...this.props} />;
    } else {
      return null;
    }
  }
};

export const getAuth = (auth) => {
  let btnArr = LocalStorage.get(BTN_PERMISSIONS);
  // auth可以是component也可以是resourceCode
  const flag = btnArr && btnArr.length > 0 && btnArr.indexOf(auth) > -1;
  return flag
};

export const AuthButton = wrapAuth(Button);
export const AuthFragment = wrapAuth(Fragment);
export const AuthMenu = wrapAuth(Menu.Item);
export const AuthTOButton = wrapAuth(TOButton);
